﻿using UnityEngine;

namespace Core
{
    public class BuffSystem : IBuff
    {
        protected int overlay = 1;
        protected float time;
        protected Unit target;
        protected BuffConfig config;

        public virtual bool IsDisposed { get; protected set; }

        void IFixedUpdate.FixedUpdate()
        {
            OnFixedUpdate();
        }

        void IBuff.Awake(BuffConfig config, Unit target)
        {
            this.target = target;
            this.config = config;
            this.IsDisposed = false;
            time = GetDurationTime();
            OnAwake();
        }

        public void Dispose()
        {
            IsDisposed = true;
            OnDispose();
        }

        void IUpdate.Update()
        {
            if (IsDisposed)
                return;

            OnUpdate();
        }

        void IBuff.AddOverlay()
        {
            overlay++;
            OnAddOverlay();
        }

        protected virtual void OnAwake()
        {

        }

        protected virtual void OnUpdate()
        {
            if (config.durationTime == 0)
                return;

            time -= Time.deltaTime;

            if (time <= 0)
            {
                IsDisposed = true;
            }

        }

        protected virtual void OnFixedUpdate()
        {

        }

        protected virtual void OnDispose()
        {
        }
        
        protected virtual void OnAddOverlay()
        {

        }

        protected float GetDurationTime()
        {
            return config.durationTime / 10000f;
        }
    }
}